<?php

namespace App\Admin\Controllers;

use App\Models\SmsModel;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class SmsController extends BaseController
{
    protected $title = '短信管理';

    /**
     * grid
     * @return Grid
     */
    protected function grid(): Grid
    {
        $grid = new Grid(new SmsModel);
        $grid->model()->orderByDesc('id');
        //$grid->expandFilter();
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('type', __('sms.type'))->select(__('sms.typeArr'));
                $filter->equal('status', __('sms.status'))->select(__('sms.statusArr'));
                $filter->where(function ($query) {
                    $query->where('created_at', '>=', $this->input);
                }, __('com.created_at') . '>=')->datetime();
                $filter->equal('ip', __('IP'));
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('mobile', __('com.mobile'))->integer();
                $filter->equal('code', __('sms.code'));
                $filter->where(function ($query) {
                    $query->where('created_at', '<=', $this->input);
                }, __('com.created_at') . '<=')->datetime();
            });
        });
        $grid->column('id', __('ID'))->sortable();
        $grid->column('type', __('sms.type'))->using(__('sms.typeArr'))->sortable();
        $grid->column('is_use', __('sms.is_use'))->using(__('com.whether'))->sortable();
        $grid->column('status', __('sms.status'))->using(__('sms.statusArr'))->sortable();
        $grid->column('code', __('sms.code'));
        $grid->column('mobile', __('com.mobile'));
        $grid->column('ip', __('IP'));
        $grid->column('created_at', __('com.created_at'))->sortable();
        return $grid;
    }

    /**
     * detail
     * @param int $id
     * @return Show
     */
    protected function detail(int $id): Show
    {
        $show = new Show(SmsModel::findOrFail($id));
        $show->field('id', __('ID'));
        $show->field('type', __('sms.type'))->using(__('sms.typeArr'));
        $show->field('is_use', __('sms.is_use'))->using(__('com.whether'));
        $show->field('status', __('sms.status'))->using(__('sms.statusArr'));
        $show->field('code', __('sms.code'));
        $show->field('mobile', __('com.mobile'));
        $show->field('ip', __('IP'));
        $show->field('content', __('sms.content'));
        $show->field('result', __('sms.result'))->unescape()->as(function ($field) {
            return encode($field);
        });
        $show->field('created_at', __('com.created_at'));
        return $show;
    }
}
